package com.example.springboot_manage.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.example.springboot_manage.result.BaseResult;
import com.example.springboot_manage.util.JsonUtil;
import com.example.springboot_manage.repository.GoodsMapper;
import com.example.springboot_manage.pojo.Goods;
import com.example.springboot_manage.pojo.GoodsExample;
import com.example.springboot_manage.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.util.HtmlUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public BaseResult saveGoods(Goods goods) {
        BaseResult baseResult = BaseResult.error();
        if (goods.getGoodsId() != null){
            GoodsExample goodsExample = new GoodsExample();
            goodsExample.createCriteria().andGoodsIdEqualTo(goods.getGoodsId());
            int row = goodsMapper.updateByExampleSelective(goods,goodsExample);
            if (row > 0) {
                baseResult = BaseResult.success();
                baseResult.setMessage(String.valueOf(goods.getGoodsId()));
            }
            return baseResult;
        }

        //如果有详情，先转义
        if (!StringUtils.isEmpty(goods.getGoodsContent())){
            goods.setGoodsContent(HtmlUtils.htmlEscape(goods.getGoodsContent(), "UTF-8"));
        }
        int result = goodsMapper.insertSelective(goods);
        //如果成功返回信息里加入插入商品的主键
        if (result>0){
            baseResult = BaseResult.success();
            baseResult.setMessage(String.valueOf(goods.getGoodsId()));
        }
        return baseResult;
    }

    @Override
    public BaseResult selectGoodsListByPage(Goods goods, Integer pageNum, Integer pageSize) {
        //定义redis Key数组
        String[] goodsKeyArr = new String[]{
                "goods:pageNum_"+pageNum+":pageSize_"+pageSize+":",
                "catId_:",
                "brandId_:",
                "goodsName_:"
        };
        PageHelper.startPage(pageNum,pageSize);
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria = example.createCriteria();
        if (goods.getCatId() != null && goods.getCatId() !=null){
            criteria.andCatIdEqualTo(goods.getCatId());
            goodsKeyArr[1] = "catId_"+goods.getCatId()+":";
        }
        if (goods.getBrandId()!=null&& goods.getBrandId()!=0){
            criteria.andBrandIdEqualTo(goods.getBrandId());
            goodsKeyArr[2] = "brandId_"+goods.getBrandId()+":";
        }
        if (!StringUtils.isEmpty(goods.getGoodsName())){
            criteria.andGoodsNameLike("%"+goods.getGoodsName()+"%");
            goodsKeyArr[3] = "goodsName_"+goods.getGoodsName()+":";
        }
        //拼接完整的RedisKey
        String goodsListKey = Arrays.stream(goodsKeyArr).collect(Collectors.joining());
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
        String pageInfoGoodsJson = valueOperations.get(goodsListKey);
        if (!StringUtils.isEmpty(pageInfoGoodsJson)){
            return BaseResult.success(JsonUtil.jsonStr2Object(pageInfoGoodsJson,PageInfo.class));
        }
        List<Goods> goods1 =  goodsMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(goods1)){
            PageInfo<Goods> pageInfo = new PageInfo<>(goods1);
            valueOperations.set(goodsListKey,JsonUtil.object2JsonStr(pageInfo));
            return BaseResult.success(pageInfo);
        }else {
            valueOperations.set(goodsListKey,JsonUtil.object2JsonStr(new PageInfo<>(new ArrayList<Goods>())),
                    60, TimeUnit.SECONDS);
        }
        return BaseResult.error();
    }
}
